<?php
/**
 * Created by PhpStorm.
 * User: 17732
 * Date: 2018/6/20
 * Time: 20:39
 */

namespace app\admin\controller;


use think\Controller;

class Publicmethod
{
    /**
     * 检测是否登陆
     */
    public static function signout($sign){
        if (!Session::get('userinfo')){
            $sign->redirect('/index/index/login');
        }
    }
    /**
     * 分页
     * @param array $data  数据数组
     * @param $page  当前页码
     */
    public static function pageing($data = array(),$page,$limit = 4){
        if(!is_array($data)) return;
        if($page < 1) return ;
        //$limit = 4;                                 // 每页显示条数
        $pageSum = ceil(count($data)/$limit);  // 总页数
        $pageArr['totalpage'] = $pageSum;
        $pageArr['currentPage'] = $page;
        if($page <= $pageSum){
            $start = ($page-1)*$limit;   // 开始位置
            $pageList = array_slice($data,$start,$limit);
            $pageArr['data'] = $pageList;
            return $pageArr;
        }
    }
    /**
     * 是否逾期未还 只返回逾期
     * @param array $data
     * @return array|void
     */
    public static function isBeOverdue($data = array())
    {
        if (!is_array($data)) return;
        $arr = [];
        foreach ($data as $k => $v) {
            // 未归还
            if($v['states'] == 1 || $v['states'] == 6){
                $now = time();   // 现在的时间戳
                if($now > $v['expectedReturnTime']){
//                    $v['isBeOverdue'] = 1;       // 1--逾期  2--未逾期
                    // 逾期时间
                    $day = ($now - $v['expectedReturnTime'])/86400;
                    $v['isBeOverdue'] = round($day);
                    $arr[] = $v;
                }
            }
            // 已归还
            if($v['states'] == 7){
                if($v['returntime'] > $v['expectedReturnTime']){
//                    $v['isBeOverdue'] = 1;       // 1--逾期  2--未逾期
                    // 逾期时间
                    $day = ($v['returntime'] - $v['expectedReturnTime'])/86400;
                    $v['isBeOverdue'] = round($day);
                    $arr[] = $v;
                }
            }
        }
        return $arr;
    }

    /**
     * 是否逾期,
     * @param array $data
     * @return array|void
     */
    public static function isBeOverdueAll($data = array())
    {
        if (!is_array($data)) return;
        $arr = [];
        foreach ($data as $k => $v) {
            // 未归还
            if($v['states'] == 1 || $v['states'] == 6){
                $now = time();   // 现在的时间戳
                if($now > $v['expectedReturnTime']){
//                    $v['isBeOverdue'] = 1;       // 1--逾期  2--未逾期
                    // 逾期时间
                    $day = ($now - $v['expectedReturnTime'])/86400;
                    $v['isBeOverdue'] = round($day);
                }
            }
            else{
                $v['isBeOverdue'] = '';
            }

            // 已归还
            if($v['states'] == 7){
                if($v['returntime'] > $v['expectedReturnTime']){
//                    $v['isBeOverdue'] = 1;       // 1--逾期  2--未逾期
                    // 逾期时间
                    $day = ($v['returntime'] - $v['expectedReturnTime'])/86400;
                    $v['isBeOverdue'] = round($day);
                }
            }
            else{
                $v['isBeOverdue'] = '';
            }
            $arr[] = $v;
        }
//        dump($arr);die;
        return $arr;
    }


}